/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.dao;

import br.com.dao.util.GenericDao;
import br.com.model.Cliente;
import br.com.servlets.CadastroCliente;
import br.com.type.Sexo;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClienteDao implements GenericDao<Cliente> {

    private Connection connection;

    public ClienteDao(Connection connection) {
        this.connection = connection;
    }

    @Override
    public void save(Cliente cliente) {

        try {
            String sql = "insert into cliente"
                    + "(numeroinscricao,nome,sexo,dataNascimento,email,ativo)"
                    + " values (?,?,?,?,?,?)";

            PreparedStatement stmt = this.connection.prepareStatement(sql);
            stmt.setString(1, cliente.getNumeroInscricao());
            stmt.setString(2, cliente.getNome());
            stmt.setString(3, cliente.getSexo().getSimbol());
            stmt.setDate(4, cliente.getDataNascimento());
            stmt.setString(5, cliente.getEmail());
            stmt.setBoolean(6, cliente.isAtivo());

            stmt.executeUpdate();
        } 
        catch (SQLException e) 
        {
        
        }

    }

    
    @Override
    public Collection<Cliente> list() {
        
        LinkedList<Cliente> list = new LinkedList<Cliente>();
        Cliente cliente;
        try {
            String sql = "select numeroinscricao,nome,sexo,dataNascimento,email,ativo "
                    + "   from cliente";

            PreparedStatement stmt = this.connection.prepareStatement(sql);
            
            ResultSet rs = stmt.executeQuery();
            while(rs.next())
            {
                cliente= new Cliente(rs.getString(1),rs.getString(2),rs.getString(3).equals("F")?Sexo.FEMININO:Sexo.MASCULINO,rs.getDate(4),rs.getString(5),rs.getBoolean(6) );
                list.add(cliente);
            }
        }
        catch (SQLException e) {
            
        }
        
        return list;
    }

    @Override
    public Cliente findById(Integer id) {
        Cliente cliente=null;
        try {
            String sql = "select numeroinscricao,nome,sexo,dataNascimento,email,ativo "
                    + "from cliente "
                    + "where numeroinscricao="+id;

            PreparedStatement stmt = this.connection.prepareStatement(sql);
            
            ResultSet rs = stmt.executeQuery();
            if(rs.next())
            cliente= new Cliente(rs.getString(1),rs.getString(2),rs.getString(3).equals("F")?Sexo.FEMININO:Sexo.MASCULINO,rs.getDate(4),rs.getString(5),rs.getBoolean(6) );
            
        }
        catch (SQLException e) {
            
        }
        
        return cliente;
    }
    
    @Override
    public void delete(Cliente cliente) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public LinkedList<Cliente> listar() {

        LinkedList<Cliente> lista = new LinkedList<>();

        String sql = "select numeroinscricao, nome, sexo, dataNascimento,email,ativo FROM cliente";

        PreparedStatement stmt;
        Cliente cl;

        try {
            stmt = this.connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                Date dataNascimento = null;
                try {
                    dataNascimento = new Date(sdf.parse(rs.getString(4)).getTime());
                } catch (ParseException ex) {
                    Logger.getLogger(CadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
                }
                cl = new Cliente(rs.getString(1), rs.getString(2), Sexo.valueOf(rs.getString(3).equals("F") ? "FEMININO" : "MASCULINO"), dataNascimento, rs.getString(5), rs.getBoolean(6));
                lista.add(cl);

            }
        } catch (SQLException ex) {
            Logger.getLogger(ClienteDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return lista;
    }

    @Override
    public Cliente getObjectById(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public LinkedList<Cliente> getObjectByName(String name) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
}
